﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Pavilion.GameTools;

namespace Pavilion.Locks
{
    class ConnectionInitiatorLock : ILock
    {
        public string GameItemName { get; set; }
        public ConnectionInitiator ConnectionInitiator { get; set; }

        public ILock Clone()
        {
            return new ConnectionInitiatorLock()
            {
                GameItemName = this.GameItemName,
                ConnectionInitiator = this.ConnectionInitiator
            };
        }

        public void Deserialize(XElement lockElement, string resourceName)
        {
            ConnectionInitiator = GameEngine.ParseObject<ConnectionInitiator>(lockElement.Attribute("ConnectionInitiator").Value);
        }

        public XElement Serialize()
        {
            XElement lockElement = new XElement("Lock");

            lockElement.Add(new XAttribute("ConnectionInitiator", ConnectionInitiator));

            return lockElement;
        }

        public LockMonitorBase CreateLockMonitor(GameItem gameItem)
        {
            ConnectionTool connectionTool = gameItem.ObtainTool<ConnectionTool>();
            return new LockPredicateMonitor(connectionTool, () => connectionTool.ConnectionInitiator == this.ConnectionInitiator, this);
        }

        public override string ToString()
        {
            return this.GetType().Name;
        }
    }
}
